package com.first.xiinn.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.first.core.dao.impl.BaseDaoImpl;
import com.first.xiinn.dao.DictionaryDao;
import com.first.xiinn.model.Dictionary;

public class DictionaryDaoImpl extends BaseDaoImpl<Dictionary>
  implements DictionaryDao
{
  public DictionaryDaoImpl()
  {
    super(Dictionary.class);
  }

  public List<Dictionary> getAllItems()
  {
    return (List)getHibernateTemplate().execute(new HibernateCallback()
    {
      public Object doInHibernate(Session session) throws HibernateException, SQLException
      {
        Query query = session.createQuery("select new Dictionary(itemTypeValue) from Dictionary group by itemTypeValue");
        return query.list();
      }
    });
  }

  public List<String> getAllByItemName(final String itemName) {
    String hql = "select itemValue from Dictionary where itemName=?";
    return (List)getHibernateTemplate().execute(new HibernateCallback()
    {
      public Object doInHibernate(Session session) throws HibernateException, SQLException
      {
        Query query = session.createQuery("select itemValue from Dictionary where itemName=?");
        query.setString(0, itemName);
        return query.list();
      }
    });
  }
}

